package com.ly.sy.controller;


import com.ly.sy.entity.*;
import com.ly.sy.service.CommentService;
import com.ly.sy.service.SyadminService;
import com.ly.sy.service.SyuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;
import java.util.ArrayList;

import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liangyu
 * @since 2020-11-11
 */
@Controller
@RequestMapping("/sy/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private SyuserService syuserService;

    //根据课程id获取父节点为0的评论
    @GetMapping("gocomment")
    public String gocomment(@RequestParam Integer courseid, HttpSession session){
        //根据课程id获取父节点为0的评论
        List<Comment> commentList = commentService.findCidAndFathernull(courseid, 0);
        for(Comment b: commentList){
            System.out.println("-----------------------------------------------------");
            System.out.println(b.getUserId());
        }
        List<reply> replies = new ArrayList<>();
        for(Comment comment: commentList){
            //获取父节点为courseid的评论
            List<Comment> listcomId = commentService.findCidAndFathernull(null, comment.getCommentId());
            Syuser syuser = commentService.findUserByid(comment.getUserId());
            reply re = new reply();
            re.setUserName(syuser.getUserName());
            re.setCommentP(comment.getCommentP());
            re.setCommentId(comment.getCommentId());
            re.setReplynum(listcomId.size());
            replies.add(re);
        }
        for(reply a: replies){
            System.out.println(a.getUserName()+":"+a.getCommentP());
            System.out.println(a.getCommentId()+":"+a.getReplynum());
            System.out.println("-----------------------------------------------------");
        }
        //评论显示内容对象
        session.setAttribute("replies",replies);
        return "/User/comment";
    }

    @GetMapping("commdetails")
    public String commdetails(@RequestParam String commentId, HttpSession session){
        //现获取本条评论的相关信息
        Comment comment = commentService.getById(commentId);
        System.out.println("===========================commentId=================================");
        System.out.println(comment.getCommentId()+"   "+commentId);
        Syuser syuser = commentService.findUserByid(comment.getUserId());
        List<Comment> listcomId = commentService.findCidAndFathernull(null, comment.getCommentId());
        reply re = new reply();
        re.setCommentId(comment.getCommentId());
        re.setUserName(syuser.getUserName());
        re.setCommentP(comment.getCommentP());
        re.setReplynum(listcomId.size());
        session.setAttribute("getoneComment",re);
        //获取回复的评论相关信息
        List<reply> replies = new ArrayList<>();
        for(Comment acomment: listcomId){
            Syuser auser = commentService.findUserByid(acomment.getUserId());
            reply onereply = new reply();
            onereply.setCommentId(acomment.getCommentId());
            onereply.setUserName(auser.getUserName());
            onereply.setCommentP(acomment.getCommentP());
            replies.add(onereply);
        }
        session.setAttribute("commentchild",replies);
        return "/User/commentdetails";
    }
    //跳转
    @GetMapping("buildcomment")
    public String buildcomment(){
        return "/User/buildcomment";
    }
    @GetMapping("gocommderails")
    public String gocommderails(){
        return "/User/commentdetails";
    }
    //发表评论
    @GetMapping("addcomment")
    @ResponseBody
    public String addcomment(@RequestParam String commentP, HttpSession session){
        Comment comment = new Comment();
        Syuser syuser = (Syuser) session.getAttribute("User");
        Course course = (Course) session.getAttribute("coursesv");
        comment.setUserId(syuser.getUserId());
        comment.setCourseId(course.getCourseId());
        comment.setCommentP(commentP);
        comment.setFather(0);
        System.out.println(comment);
        boolean ss = commentService.save(comment);
        System.out.println(ss);
        reply rep = new reply();
        rep.setReplynum(0);
        rep.setUserName(syuser.getUserName());
        rep.setCommentP(commentP);
        session.setAttribute("getoneComment",rep);
        return "/sy/comment/gocommderails";
    }

    //发表回复
    @GetMapping("replycomment")
    @ResponseBody
    public String replycomment(@RequestParam String commentP, HttpSession session){
        Comment comment = new Comment();
        Syuser syuser = (Syuser) session.getAttribute("User");
        Course course = (Course) session.getAttribute("coursesv");
        reply onecomment = (reply) session.getAttribute("getoneComment");

        System.out.println("===========================commentId=two=================================");
        System.out.println(onecomment.getCommentId());

        comment.setFather(onecomment.getCommentId());
        comment.setCommentP(commentP);
        comment.setCourseId(course.getCourseId());
        comment.setUserId(syuser.getUserId());
        boolean save = commentService.save(comment);
        System.out.println(save);
        return "/sy/comment/commdetails?commentId="+onecomment.getCommentId();
    }


//    ************************************admin******************************************
    @GetMapping("commentadmin")
    public String commentadmin(HttpSession session){
        List<Comment> commentList = commentService.list(null);
        if(!ObjectUtils.isEmpty(commentList)){
            List<Commentadmin> commentadminList = new ArrayList<>();
            for (Comment comment: commentList){
                Syuser syuser = syuserService.getById(comment.getUserId());
                Commentadmin commentadmin = new Commentadmin();
                commentadmin.setCommentId(comment.getCommentId());
                commentadmin.setUserId(comment.getUserId());
                commentadmin.setUserImg(syuser.getUserImg());
                commentadmin.setUserName(syuser.getUserName());
                commentadmin.setCourseId(comment.getCourseId());
                commentadmin.setCommentP(comment.getCommentP());
                commentadmin.setFather(comment.getFather());
                commentadminList.add(commentadmin);
            }
            session.setAttribute("commentadminList",commentadminList);
        }
        return "/admin/commentadmin";
    }
// 评论搜索
    @GetMapping("search")
    @ResponseBody
    public List<Commentadmin> search(
            @RequestParam String searchstr
    ){
        List<Comment> comment1 = commentService.commentlikeLise(searchstr);
        List<Commentadmin> commentadminList = new ArrayList<>();
        if(!ObjectUtils.isEmpty(comment1)){
            for (Comment comment: comment1){
                Syuser syuser = syuserService.getById(comment.getUserId());
                Commentadmin commentadmin = new Commentadmin();
                commentadmin.setCommentId(comment.getCommentId());
                commentadmin.setUserId(comment.getUserId());
                commentadmin.setUserImg(syuser.getUserImg());
                commentadmin.setUserName(syuser.getUserName());
                commentadmin.setCourseId(comment.getCourseId());
                commentadmin.setCommentP(comment.getCommentP());
                commentadmin.setFather(comment.getFather());
                commentadminList.add(commentadmin);
            }
        }
        return commentadminList;
    }
    //评论删除
    @PostMapping("deletecomment")
    @ResponseBody
    public boolean deletecomment(
            @RequestParam Integer commentId
    ){

        return commentService.removeById(commentId);
    }
}

